package org.dromara.admin.service.impl;

import org.dromara.admin.domain.vo.EmployeeVo;
import org.dromara.admin.service.IEmployeeService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.admin.domain.bo.UserRemarkBo;
import org.dromara.admin.domain.vo.UserRemarkVo;
import org.dromara.admin.domain.UserRemark;
import org.dromara.admin.mapper.UserRemarkMapper;
import org.dromara.admin.service.IUserRemarkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户备注Service业务层处理
 *
 * @author Lion Li
 * @date 2025-11-03
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserRemarkServiceImpl implements IUserRemarkService {

    private final UserRemarkMapper baseMapper;
    private final IEmployeeService employeeService;

    /**
     * 查询用户备注
     *
     * @param id 主键
     * @return 用户备注
     */
    @Override
    public UserRemarkVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用户备注列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户备注分页列表
     */
    @Override
    public TableDataInfo<UserRemarkVo> queryPageList(UserRemarkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserRemark> lqw = buildQueryWrapper(bo);
        Page<UserRemarkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自己公司的备注列表
     *
     * @param userId 用户ID
     * @return 用户备注列表
     */
    @Override
    public List<UserRemarkVo> queryMyList(Long userId) {
        if (userId == null) {
            return null;
        }
        // 获取公司ID
        Long companyId = employeeService.queryCompanyIdByUserId(userId);
        return baseMapper.selectVoList(
            new LambdaQueryWrapper<UserRemark>()
                .eq(UserRemark::getCompanyId, companyId)
                .orderByDesc(UserRemark::getCreateTime));
    }

    /**
     * 查询符合条件的用户备注列表
     *
     * @param bo 查询条件
     * @return 用户备注列表
     */
    @Override
    public List<UserRemarkVo> queryList(UserRemarkBo bo) {
        LambdaQueryWrapper<UserRemark> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserRemark> buildQueryWrapper(UserRemarkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserRemark> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(UserRemark::getId);
        lqw.eq(bo.getUserId() != null, UserRemark::getUserId, bo.getUserId());
        lqw.eq(bo.getCompanyId() != null, UserRemark::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarkContent()), UserRemark::getRemarkContent, bo.getRemarkContent());
        return lqw;
    }

    /**
     * 新增用户备注
     *
     * @param bo 用户备注
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserRemarkBo bo) {
        UserRemark add = MapstructUtils.convert(bo, UserRemark.class);

        // 获取公司ID并设置
        Long companyId = employeeService.queryCompanyIdByUserId(add.getUserId());
        add.setCompanyId(companyId);

        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户备注
     *
     * @param bo 用户备注
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserRemarkBo bo) {
        UserRemark update = MapstructUtils.convert(bo, UserRemark.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserRemark entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用户备注信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
